package com.documents.service.users;

import com.documents.common.core.PageBean;
import com.documents.common.core.service.BaseSrv;
import com.documents.pojo.Users;
import com.opensymphony.xwork2.ActionContext;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class UserService extends BaseSrv<Users, Integer> {

    private static final Log log = LogFactory.getLog(UserService.class);

    public PageBean queryForPage(int pageSize, int page, Users users) {

        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);
        pageBean.setCurrentPage(page);
        String condition = "";
        Object[] parameters = new Object[1];
        int paramIndex = 0;

        Long allRow = this.findTotalCount(condition, null);
        int totalPage = PageBean.countTotalPage(pageSize, allRow.intValue()); // 总页数
        pageBean.setAllRow(allRow.intValue());
        pageBean.setTotalPage(totalPage);

        List<Users> list = new ArrayList();
        if (page == 1) {
            list = this.findWithPaging(condition, parameters, "id", page - 1, pageSize);
        } else {
            list = this.findWithPaging(condition, parameters, "id", (page - 1) * pageSize, pageSize);
        }
        pageBean.setList(list);
        return pageBean;
    }

    public boolean del(int id) {
        this.delById(id);
        return true;
    }
       

    public String validateLogin(String username, String password) {

        if (null == username || username.equals("")) {
            Map session = ActionContext.getContext().getSession();
            Users users = (Users) session.get("user");
            if (null == users) {
                return "login";
            } else {
                username = users.getLoginName();
                password = users.getPassword();
            }
        }
        log.info("获取用户信息！" + username + "~~~~~" + password);
        String result = "login";

        Users u = this.findUnique("login_name=?", new Object[]{username});

        if (u != null) {
            if (password.equalsIgnoreCase(u.getPassword())) {
                result = u.getRole().toString();
                ActionContext.getContext().getSession().put("user", u);
            } else {
                result = "login";
            }
        }
        return result;
    }
}
